<?php
// -----------------------------------------------------------------------------

if (!isset($_GET['from'])) {
    exit("需要指定完整的参数。");
}

$from = htmlspecialchars($_GET['from']);

// -----------------------------------------------------------------------------
include "global.php";
include "auth.php";
// -----------------------------------------------------------------------------

$content = "";

$hiddenHtml = <<<BBB
    <input type="hidden" id="inputFrom" name="from" value="$from">
BBB;

switch ($from) {
    case "goals":
        $content = $db->readFromGoals();
        break;
    case "storageBox":
        $content = $db->readFromStorageBox();
        break;
    case "daily":
        $content = $db->readFromDaily();
        break;
    case "weekly":
        if (!isset($_GET['dayOfWeek'])) {
            exit("参数不完整。");
        }
        $dayOfWeek = htmlspecialchars($_GET['dayOfWeek']);
        $content = $db->readFromWeekly($dayOfWeek);

        $hiddenHtml .= <<<BBB
            <input type="hidden" name="dayOfWeek" value="$dayOfWeek">
        BBB;

        break;
    case "monthly":
        if (!isset($_GET['dayOfMonth'])) {
            exit("参数不完整。");
        }
        $dayOfMonth = htmlspecialchars($_GET['dayOfMonth']);
        $content = $db->readFromMonthly($dayOfMonth);

        $hiddenHtml .= <<<BBB
            <input type="hidden" name="dayOfMonth" value="$dayOfMonth">
        BBB;

        break;
    case "yearly":
        if (!isset($_GET['monthAndDay'])) {
            exit("参数不完整。");
        }
        $monthAndDay = htmlspecialchars($_GET['monthAndDay']);
        $content = $db->readFromYearly($monthAndDay);

        $hiddenHtml .= <<<BBB
            <input type="hidden" name="monthAndDay" value="$monthAndDay">
        BBB;

        break;
    case "specificDate":
        if (!isset($_GET['date'])) {
            exit("参数不完整。");
        }
        $date = htmlspecialchars($_GET['date']);
        $content = $db->readFromSpecificDate($date);

        $hiddenHtml .= <<<BBB
            <input type="hidden" name="date" value="$date">
        BBB;

}
// -----------------------------------------------------------------------------
?>
<html>
<head>
    <meta charset="utf-8">
    <title>编辑</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="resources/bootstrap-5.2.0-beta1-dist/css/bootstrap.min.css">
</head>
<body>

<!-- ----------------------------------------------------------------------- -->
<?php include "globalNavbar.php"; ?>
<!-- ----------------------------------------------------------------------- -->

<div class="container container-fluid mt-3">
    <form action="formAction.php" method="post" id="myForm">

        <div>
            <?php echo $hiddenHtml; ?>
        </div>

        <div>
            <textarea name="content"
                      id="inputContent"
                      class="form-control"
                      autofocus
                      cols="30"
                      rows="15"><?php echo $content; ?></textarea>
        </div>
        <div class="mt-2">
            <button id="btnSubmit" type="button" class="btn btn-outline-primary">保存</button>
            <button id="btnReset" type="button" class="btn btn-outline-secondary">清空</button>
            <button id="btnXiangxian" type="button" class="btn btn-outline-secondary">四象限</button>
        </div>
    </form>
</div>

<!-- ----------------------------------------------------------------------- -->

<!--消息弹窗-->
<div aria-live="polite" aria-atomic="true" class="position-relative">
    <div class="toast-container position-fixed top-0 start-50 translate-middle-x">

        <div id="toastOne" class="toast" role="alert" aria-live="assertive" aria-atomic="true">
            <div class="toast-header bg-info">
                <strong class="me-auto">提示</strong>
                <button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
            </div>
            <div class="toast-body bg-white">
                <p id="msgContent">数据保存成功！</p>
            </div>
        </div>

    </div>
</div>

<!-- ----------------------------------------------------------------------- -->
<script src="resources/jQuery/jquery-3.5.1.js"></script>
<script src="resources/bootstrap-5.2.0-beta1-dist/js/bootstrap.min.js"></script>
<!-- ----------------------------------------------------------------------- -->

<script>

    $("#btnSubmit").click(function () {

        var url = "formAction.php";

        $.post(url, $("#myForm").serialize())
            .done(function (data) {
                $("#msgContent").text(data)
                const toast = new bootstrap.Toast(toastOne, {"delay": 1000})
                toast.show()
            })
            .fail(function () {
                $("#msgContent").text("请求失败！")
                const toast = new bootstrap.Toast(toastOne, {"delay": 1000})
                toast.show()
            })

    })

    $("#btnReset").click(function () {
        $("#inputContent").val("")
        $("#inputContent").focus()
    })

</script>

<!-- ----------------------------------------------------------------------- -->

<script>
    // 按下 ctrl + s 时保存。
    // 参考资料：https://stackoverflow.com/questions/93695/best-cross-browser-method-to-capture-ctrls-with-jquery
    $(window).bind('keydown', function(event) {
        if (event.ctrlKey || event.metaKey) {
            switch (String.fromCharCode(event.which).toLowerCase()) {
                case 's':
                    event.preventDefault();
                    $("#btnSubmit").click();
            }
        }
    });
</script>

<!-- ----------------------------------------------------------------------- -->

<script>
    var formEdited = false;
    // 监听用户是进行了输入操作
    $('#inputContent').keydown(function () {
        formEdited = true;
    });

    // 页面刷新或关闭前弹窗警告
    $(window).on('beforeunload', function(){
        if (formEdited == true) {
            return "Are you sure you want to leave?";
        }
    });

    // 点击按钮提交表单刷新页面时不弹窗警告
    $('#btnSubmit').click(function () {
        $(window).off('beforeunload');
    });

</script>

<!-- ----------------------------------------------------------------------- -->

<script>

    $("#btnXiangxian").click(function () {
        var str = `# ———————————————————————
# 重要，紧急 ★★★★
# ———————————————————————

待办事项1
待办事项2


# ———————————————————————
# 重要，不紧急 ★★★✰
# ———————————————————————


# ———————————————————————
# 不重要，紧急 ★★✰✰
# ———————————————————————


# ———————————————————————
# 不重要，不紧急 ★✰✰✰
# ———————————————————————


# ———————————————————————
# 其他 ✰✰✰✰
# ———————————————————————


`

        $("#inputContent").val(str)
        $("#inputContent").focus()

    })
</script>

<!-- ----------------------------------------------------------------------- -->

</body>
</html>